home *** CD-ROM | disk | FTP | other *** search
/ Scene 96 / Scene 96 International Edition (Zyklop Software) (Disc 2) (1997).iso / misc / coding / cp2dekit / samples / arcrar.cpp next >
C/C++ Source or Header  |  1996-12-29  |  4KB  |  155 lines

  1. //***************************************************************************
  2. //
  3. // this file is (c) '94-'96 Niklas Beisert
  4. //
  5. // this file is part of the cubic player development kit.
  6. // you may only use/modify/spread this file under the terms stated
  7. // in the cubic player development kit accompanying documentation.
  8. //
  9. //***************************************************************************
  10.  
  11. //cp.ini:
  12. //[defaultconfig]
  13. //  link=arcrar
  14. //[fileselector]
  15. //  arcs=_adbRARReg
  16.  
  17.  
  18. // rar archive reader
  19. // example for archive readers
  20.  
  21. #include <io.h>
  22. #include <stdlib.h>
  23. #include <string.h>
  24. #include "binfile.h"
  25. #include "pfilesel.h"
  26. #include "psetting.h"
  27.  
  28. static int adbRARScan(const char *path)
  29. {
  30.   char ext[_MAX_EXT];
  31.   char name[_MAX_FNAME];
  32.   char arcname[12];
  33.  
  34.   _splitpath(path, 0, 0, name, ext);
  35.   fsConvFileName12(arcname, name, ext);
  36.  
  37.   sbinfile file;
  38.   if (!file.open(path, sbinfile::openro))
  39.     return 1;
  40.   arcentry a;
  41.   memcpy(a.name, arcname, 12);
  42.   a.size=file.length();
  43.   a.flags=ADB_ARC;
  44.   if (!adbAdd(a))
  45.     return 0;
  46.   unsigned short arcref=adbFind(arcname);
  47.  
  48.   unsigned char markerblock[7];
  49.   if (!file.eread(markerblock, 7))
  50.     return 0;
  51.   if (memcmp(markerblock, "\x52\x61\x72\x21\x1a\x07\x00", 7))
  52.     return 0;
  53.  
  54.   file.gets();
  55.   if (file.getc()!=0x73)
  56.     return 0;
  57.   unsigned short hdflags=file.getus();
  58.   if (hdflags&0x8000)
  59.     return 0;
  60.   file.seekcur(file.getus()-7);
  61.  
  62.   while (1)
  63.   {
  64.     struct
  65.     {
  66.       unsigned short crc;
  67.       unsigned char type;
  68.       unsigned short flags;
  69.       unsigned short blklen;
  70.     } rarhead;
  71.  
  72.     unsigned long nextpos=file.tell();
  73.     if (!file.eread(&rarhead, 7))
  74.       return 1;
  75.     nextpos+=rarhead.blklen;
  76.     unsigned long extra=0;
  77.     if (rarhead.flags&0x8000)
  78.       extra=file.getul();
  79.     nextpos+=extra;
  80.     if ((rarhead.type!=0x74)||(rarhead.flags&5))
  81.     {
  82.       file.seek(nextpos);
  83.       continue;
  84.     }
  85.     unsigned long uncompsize=file.getul();
  86. /*
  87.     file.getc(); // os
  88.     file.getl(); // crc
  89.     file.getl(); // time
  90.     file.getc(); // ver
  91.     file.getc(); // meth
  92. */
  93.     file.seekcur(11);
  94.     unsigned short namelen=file.getus();
  95.     unsigned long attr=file.getul();
  96.     char filename[256];
  97.     if (namelen>255)
  98.     {
  99.       file.seek(nextpos);
  100.       continue;
  101.     }
  102.     file.read(filename, namelen);
  103.     filename[namelen]=0;
  104.     strupr(filename);
  105.     _splitpath(filename, 0, 0, name, ext);
  106.     if (fsIsModule(ext))
  107.     {
  108.       a.size=uncompsize;
  109.       a.parent=arcref;
  110.       a.flags=0;
  111.       fsConvFileName12(a.name, name, ext);
  112.       if (!adbAdd(a))
  113.         return 0;
  114.     }
  115.     file.seek(nextpos);
  116.   }
  117.   file.close();
  118.   return 1;
  119. }
  120.  
  121. static int adbRARCall(int act, const char *apath, const char *file, const char *dpath)
  122. {
  123.   switch (act)
  124.   {
  125.   case adbCallGet:
  126.     return !adbCallArc(cfGetProfileString("arcRAR", "get", "rar e -std %a %d %n"), apath, file, dpath);
  127.     break;
  128.   case adbCallPut:
  129.     return !adbCallArc(cfGetProfileString("arcRAR", "put", "rar a -std -ep %a %n"), apath, file, dpath);
  130.     break;
  131.   case adbCallDelete:
  132.     return !adbCallArc(cfGetProfileString("arcRAR", "delete", "rar d -std %a %n"), apath, file, dpath);
  133.     break;
  134.   case adbCallMoveTo:
  135.     if (cfGetProfileString("arcRAR", "moveto", 0))
  136.       return !adbCallArc(cfGetProfileString("arcRAR", "moveto", "rar m -std -ep %a %n"), apath, file, dpath);
  137.     if (!adbRARCall(adbCallPut, apath, file, dpath))
  138.       return 0;
  139.     unlink(file);
  140.     return 1;
  141.   case adbCallMoveFrom:
  142.     if (cfGetProfileString("arcRAR", "movefrom", 0))
  143.       return !adbCallArc(cfGetProfileString("arcRAR", "movefrom", ""), apath, file, dpath);
  144.     if (!adbRARCall(adbCallGet, apath, file, dpath))
  145.       return 0;
  146.     return adbRARCall(adbCallDelete, apath, file, dpath);
  147.   }
  148.   return 0;
  149. }
  150.  
  151. extern "C"
  152. {
  153.   adbregstruct adbRARReg = {".RAR", adbRARScan, adbRARCall};
  154. };
  155.